package com.songcha.library_common.util

import android.app.Activity
import android.content.Context
import android.graphics.Point
import android.os.Build
import android.util.DisplayMetrics
import android.view.WindowManager
import com.songcha.library_common.LibraryCommon


fun Int.dp2px():Int{
    return ScreenUtil.dp2px(this.toFloat()).toInt()
}

fun Int.px2dp():Int{
    return ScreenUtil.px2dp(this.toFloat()).toInt()
}

fun Int.sp2px():Int{
    return ScreenUtil.sp2px(this.toFloat()).toInt()
}

fun Int.px2sp():Int{
    return ScreenUtil.px2sp(this.toFloat()).toInt()
}


fun Float.dp2px():Float{
    return ScreenUtil.dp2px(this)
}

fun Float.px2dp():Float{
    return ScreenUtil.px2dp(this)
}

fun Float.sp2px():Float{
    return ScreenUtil.sp2px(this)
}

fun Float.px2sp():Float{
    return ScreenUtil.px2sp(this)
}

class ScreenUtil {
    companion object{

        @JvmStatic
        fun getDensity():Float{
            return LibraryCommon.getContext().resources.displayMetrics.density
        }

        @JvmStatic
        fun getDensityDpi():Int{
            return LibraryCommon.getContext().resources.displayMetrics.densityDpi
        }

        @JvmStatic
        fun px2dp(px:Float):Float{
            if(px==0f) return 0f
            val  scale:Float =  LibraryCommon.getContext().resources.displayMetrics.density
            return  (px / scale)//+ 0.5f
        }

        @JvmStatic
        fun dp2px(dp:Float):Float{
            if(dp==0f) return 0f
            val scale:Float = LibraryCommon.getContext().resources.displayMetrics.density
            return (dp * scale)//+ 0.5f
        }

        //scaledDensity = density*fontScale
        @JvmStatic
        fun px2sp(px:Float):Float{
            if(px==0f) return 0f
            val  scale:Float =  LibraryCommon.getContext().resources.displayMetrics.scaledDensity
            return  (px / scale)//+ 0.5f
        }

        @JvmStatic
        fun sp2px(sp:Float):Float{
            if(sp==0f) return 0f
            val  scale:Float =  LibraryCommon.getContext().resources.displayMetrics.scaledDensity
            return  (sp * scale)//+ 0.5f
        }

        @JvmStatic
        fun getScreenWidth():Int{
            //val dm=LibraryCommon.getContext().resources.displayMetrics
            //return dm.widthPixels
            return getScreenWidth(LibraryCommon.getContext())
        }

        /***
         * show height,not include bottom bar height
         * some device not include status height
         */
        @JvmStatic
        fun getScreenHeight():Int{
            //val dm=LibraryCommon.getContext().resources.displayMetrics
            //return dm.heightPixels
            return getScreenHeight(LibraryCommon.getContext())
            //return getScreenRealHeight()-StatusBarUtil.getBottomBarHeight()
        }


        @JvmStatic
        fun getScreenRealHeight():Int{
            val manager = LibraryCommon.getContext().getSystemService(Context.WINDOW_SERVICE) as WindowManager
            val defaultDisplay = manager.defaultDisplay
            val realPoint = Point()
            defaultDisplay.getRealSize(realPoint)
            return realPoint.y
        }

        @JvmStatic
        fun getScreenWidth(context:Context):Int{
            val dm=context.resources.displayMetrics
            return dm.widthPixels
        }

        @JvmStatic
        fun getScreenHeight(context:Context):Int{
            if(isAllScreenDevice(context)){
                var sRealSizes = arrayOfNulls<Point>(2)
                var orientation = context.resources?.configuration?.orientation
                orientation = if (orientation == 1) 0 else 1
                if (sRealSizes[orientation] == null) {
                    val windowManager = context.getSystemService(Context.WINDOW_SERVICE) as WindowManager
                    val display = windowManager.defaultDisplay
                    val point = Point()
                    display.getRealSize(point)
                    sRealSizes[orientation] = point
                }
                return sRealSizes[orientation]?.y!!-StatusBarUtil.getBottomBarHeight()
            }else{
                val dm=context.resources.displayMetrics
                return dm.heightPixels
            }
        }

        @JvmStatic
        fun setScreenBrightness(context:Activity,brightness:Float){
            if(brightness<0f || brightness>1f) return
            val window = context.window
            val lp = window.attributes
            lp.screenBrightness = brightness
            window.attributes=lp
        }

        @JvmStatic
        fun setAutoBrightness(context:Activity){
            val window = context.window
            val lp = window.attributes
            lp.screenBrightness = WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_NONE
            window.attributes=lp
        }


        @JvmStatic
        fun isAllScreenDevice(context: Context): Boolean {
            if (Build.VERSION.SDK_INT < 21) {
                return false
            } else {
                val windowManager = context.getSystemService(Context.WINDOW_SERVICE) as WindowManager
                val display = windowManager.defaultDisplay
                val point = Point()
                display.getRealSize(point)
                val width: Float
                val height: Float
                if (point.x < point.y) {
                    width = point.x.toFloat()
                    height = point.y.toFloat()
                } else {
                    width = point.y.toFloat()
                    height = point.x.toFloat()
                }
                if (height / width >= 1.97f) {
                    return true
                }
                return false
            }
        }



    }


}